﻿@using Pear.Web.Core
@inherits PearRazorPage<object>
@{ Layout = "~/Views/Shared/_Layout.cshtml"; }
@{ ViewData["Title"] = "用户分配";}
@section header
{

}
<form class="layui-form" action="" lay-filter="user-edit">
    <div class="mainBox">
        <div class="main-container">
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="user-give">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
@section footer
{
    <script>
        layui.use(['form', 'jquery', 'http', 'popup'], function () {
            let form = layui.form;
            let $ = layui.jquery;
            let http = layui.http;
            let popup = layui.popup;


            http.get('/api/role/list').then((result) => {
                for (i = 0; i < result.data.items.length; i++) {
                    var item = result.data.items[i];
                    $(".main-container")
                        .append('<div class="layui-form-item">' +
                            '<label class="layui-form-label"></label>' +
                            '<div class="layui-input-block">' +
                            '<input type="checkbox" name="role" title="' + item.name + '" value="' + item.id + '" lay-skin="primary">' +
                            '</div>' +
                            '</div>')
                }
                form.render();
                http.get('/api/user/@GetValue("id","0")/roles').then((result2) => {
                    for (i = 0; i < result2.data.length; i++) {
                        var item = result2.data[i];
                        $("[name='role'][value='" + item.id + "']").attr("checked", true);
                    }
                    form.render();
                });
            });


            form.on('submit(user-give)', function (data) {
                let roleIds = "";
                $('input[type=checkbox]:checked').each(function () {
                    roleIds += $(this).val() + ",";
                });
                roleIds = roleIds.substr(0, roleIds.length - 1);
                var saveIds = new Array();
                saveIds = roleIds.split(",");
                http.post('/api/user/give/@GetValue("id","0")', JSON.stringify(saveIds)).then((result) => {
                    if (result.succeeded) {
                        popup.success("修改成功", function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("user-table");
                        });
                    } else {
                        popup.failure("修改失败");
                    }
                });
                return false;
            });
        })
    </script>
}